var gulp = require("gulp");
var browserify = require("browserify");
var source = require('vinyl-source-stream');
var watchify = require("watchify");
var tsify = require("tsify");
var gutil = require("gulp-util");

var paths = {
  pages: ['src/*.html']
};

var watchedBrowserify = watchify(browserify({
  basedir: '.',
  debug: true,
  entries: ['src/main.ts'],
  cache: {},
  packageCache: {}
}).plugin(tsify));

gulp.task("copy-html", function () {
  return gulp.src(paths.pages)
    .pipe(gulp.dest("dist"));
});


var bundle = function () {
  return watchedBrowserify.bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest("dist"));
}


//构建任务 
gulp.task('default', gulp.series('copy-html', bundle));

watchedBrowserify.on("update", bundle);
watchedBrowserify.on("log", gutil.log);